package com.maila.xiaoaoschool.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.api.LoginApi;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by mark on 2017/12/13.
 */

public class NewPayPasswordActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.btn_finish)
    Button btnFinish;
    @BindView(R.id.tv_zhanghao)
    TextView tvZhanghao;
    @BindView(R.id.delete_text_iv)
    ImageView deleteTextIv;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_pay_password);
        ButterKnife.bind(this);
        btnFinish.setEnabled(false);
        etNewPassword.setFocusable(true);
        etNewPassword.setFocusableInTouchMode(true);
        etNewPassword.requestFocus();
        InputMethodManager inputManager =
                (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(etNewPassword, InputMethodManager.RESULT_SHOWN);
        inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

        etNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() > 6) {
                    deleteTextIv.setVisibility(View.VISIBLE);
                    btnFinish.setEnabled(true);
                    btnFinish.setBackgroundResource(R.drawable.bg_rd_orange);
                } else {
                    deleteTextIv.setVisibility(View.INVISIBLE);
                    btnFinish.setEnabled(false);
                    btnFinish.setBackgroundResource(R.drawable.bg_rd_gray);
                }
            }
        });
    }

    @OnClick({R.id.white_return_iv, R.id.btn_finish, R.id.delete_text_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.btn_finish:
                new LoginApi(this, new VolleyInterface() {
                    @Override
                    public void onSuccess(String result) {
                        try {
                            JSONObject jsonObject=new JSONObject(result);
                            int code=jsonObject.optInt("result");
                            if(code==1){
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(VolleyError error) {

                    }
                }).set_pay_pwd(etNewPassword.getText().toString());
                break;
            case R.id.delete_text_iv:
                etNewPassword.setText("");
                break;
        }
    }
}
